core.Box.prototype.dd = true;

core.Box.prototype.enableDragging = function(aAreaComponent, aElementToDetect, aElementToClone){
	this.draggable = true;
	this.dropArea = aAreaComponent;
	this.dragReady = false;
    this.elementToClone = aElementToClone || this.div;
	var instance = this;
	if(!aElementToDetect)
		aElementToDetect = this;
//	core.log('enable dragging for', this.className, 'over', this.dropArea.className, 'using', aElementToDetect.className);
	aElementToDetect.on('mousedown', function(i, e){
//		core.log('start drag from ', e.clientX, e.clientY, e);
		instance.dragReady = true;
		instance.dragStartX = e.clientX;
		instance.dragStartY = e.clientY;
	});
	
	aElementToDetect.on('mousemove', function(i, e){
		if(instance.dragReady && instance.draggable && instance.dropArea){
			instance.dropArea.enableDragArea(instance);
		}
	});
	
	aElementToDetect.on('mouseup', function(i, e){
		instance.dragReady = false;
	});
	
	aElementToDetect.on('mouseout', function(i, e){
		instance.dragReady = false;
	});
}

core.Box.prototype.enableDragArea = function(aDraggable){
	this.draggable = aDraggable;
	this.dropTarget = null;
	this.dragElementX = Math.floor($(aDraggable.elementToClone).width()/2);
	this.dragElementY = Math.floor($(aDraggable.elementToClone).height()/2);
	this.dragElement = $(aDraggable.elementToClone).clone().appendTo(this.div);
	$(this.dragElement).hide();
	this.dragArea = core.newDiv(this.div, 'split_layer');
	$(this.dragArea).width(this.bounds.width);
	$(this.dragArea).height(this.bounds.height);
	$(this.dragArea).show();
	var instance = this;
	$(this.dragArea).mouseup(function(e){
//		core.log('mouseup, stop dragging');
		instance.stopDragArea(true);
		return false;
	});
	
	$(this.dragArea).mousemove(function(e){
		$(instance.dragElement).show();
        var off = $(instance.dragArea).offset();
//        core.log(e.clientX, instance.dragElementX, e.clientY, instance.dragElementY, e);
		$(instance.dragElement).css('left', e.clientX-instance.dragElementX-off.left);
		$(instance.dragElement).css('top', e.clientY-instance.dragElementY-off.top);
		var offset = $(instance.div).offset();
		var dropTarget = instance.getDropTarget(instance.draggable, e.clientX-offset.left, e.clientY-offset.top);
		if(instance.dropTarget!=dropTarget){
			if(instance.dropTarget)
				instance.dropTarget.unmarkAsDropTarget();
			if(dropTarget)
				dropTarget.markAsDropTarget(instance.draggable);
			instance.dropTarget = dropTarget;
//            $(instance.dragArea).css('cursor', instance.dropTarget?'pointer':'crosshair');
		}
		return false;
	});
	
	$(this.dragArea).mouseout(function(e){
//		core.log('mouseout, stop dragging');
		instance.stopDragArea(false);
		return false;
	});
}

core.Box.prototype.stopDragArea = function(aSuccess){
	if(this.dropTarget)
		this.dropTarget.unmarkAsDropTarget();
	if(this.dragElement){
		$(this.dragElement).remove();
		this.dragElement = null;
	}
	if(this.dragArea){
		$(this.dragArea).remove();
		this.dragArea = null;
	}
	if(this.dropTarget && this.draggable && aSuccess)
		this.dropTarget.acceptDraggable(this.draggable);
	this.draggable = null;
}

core.Box.prototype.getDropTarget = function(aDraggable, aX, aY){
	if(!this.items)
		return null;
	for(var i = 0; i<this.items.length; i++){
        var x = aX+$(this.div).scrollLeft();
        var y = aY+$(this.div).scrollTop();
		var itm = this.items[i];
		if(itm.x<=x && itm.y<=y && itm.x+itm.width>=x && itm.y+itm.height>=y && itm.getDropTarget){
			return itm.getDropTarget(aDraggable, x-itm.x, y-itm.y);
		}
	}
	return null;
}

core.Box.prototype.acceptDraggable = function(aDraggable){
	
}

core.Box.prototype.markAsDropTarget = function(aDropable){
	this.markedAsDropable = true;
	this.dropableCandidate = aDropable;
	this.dropTargetDiv = core.newDiv(this.div, 'desktop_disable');
	$(this.dropTargetDiv).width(this.bounds.width).height(this.bounds.height).css('left', 0).css('top', 0);
    //.
//        css('left', this.bounds.x).css('top', this.bounds.y)
};

core.Box.prototype.unmarkAsDropTarget = function(){
	this.markedAsDropable = false;
	if(this.dropTargetDiv){
		$(this.dropTargetDiv).remove();
		this.dropTargetDiv = null;
	}
};
